﻿using Microsoft.AspNetCore.Mvc;
using NetCoreApi.Controllers.Dto;
using System.Collections.Generic;
using System.IO;
using System.Reflection.Emit;
using System.Security.Principal;

namespace NetCoreApi.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class UserController : ControllerBase
    {
        [HttpGet]
        public Res GetList([FromQuery] PageDto dto)
        {
            if (dto.Limit == 0)
                dto.Limit = 20;
            if (_list.Count() == 0)
            {
                _list = GenFu.GenFu.ListOf<UserInfo>(200);
            }
            var LIST = new List<UserInfo>();
            if (string.IsNullOrWhiteSpace(dto.Name))
                LIST = _list.Skip(dto.Page * dto.Limit).Take(dto.Limit).ToList();
            else
                LIST = _list.Where(m => m.Name.Contains(dto.Name)).Skip(dto.Page - 1 * dto.Limit).Take(dto.Limit).ToList();

            return new Res { Code = 200, Data = new Data { Count = 200, List = LIST } };
        }

        [HttpGet("{Id}")]
        public Res Get(int id)
        {
            //_list = GenFu.GenFu.ListOf<UserInfo>(20);
            _list.FirstOrDefault(m => m.Id == id);
            return new Res { Code = 200, Data = new Data { }, Mes = "查询成功。" };
        }

        [HttpDelete]
        public Res Delete(int id)
        {
            //_list = GenFu.GenFu.ListOf<UserInfo>(20);
            var info = _list.FirstOrDefault(m => m.Id == id);
            _list.Remove(info);

            return new Res { Code = 200, Data = new Data { }, Mes = "删除成功。" };
        }

        [HttpPut]
        public Res Update(UserInfo info)
        {
            //_list = GenFu.GenFu.ListOf<UserInfo>(20);
            var item = _list.FirstOrDefault(m => m.Id == info.Id);
            _list.Remove(item);
            _list.Add(info);
            return new Res { Code = 200, Data = new Data { }, Mes = "修改成功。" };
        }

        [HttpPost]
        public Res Insert(UserInfo info)
        {
            //_list = GenFu.GenFu.ListOf<UserInfo>(20);
            //var info = _list.FirstOrDefault(m => m.StudentId == id);
            _list.Add(info);
            return new Res { Code = 200, Data = new Data { }, Mes = "添加成功。" };
        }

        [HttpPost("getMenu")]
        public Res GetMenu(LoginInfo info)
        {
            var menu = new List<Menu>();
            if (info.UserName == "admin")
            {
                menu.Add(new Menu { Path = "/home", Name = "home", Label = "首页", Icon = "house", Url = "home/Home" });
                menu.Add(new Menu { Path = "/mall", Name = "mall", Label = "商品管理", Icon = "video-play", Url = "mall/Index" });
                menu.Add(new Menu { Path = "/user", Name = "user", Label = "用户管理", Icon = "user", Url = "user/User" });
                menu.Add(new Menu
                {
                    Label = "其他",
                    Icon = "location",
                    Children = (new List<Menu> {
                  new Menu  {Path = "/page1", Name = "page1", Label = "页面1", Icon = "setting", Url = "page1" },
                  new Menu  {Path = "/page2", Name = "page2", Label = "页面2", Icon = "setting", Url = "page2" },
                }).ToArray()
                });
                return new Res { Code = 200, Data = new Data { Menu = menu, Token = Guid.NewGuid().ToString("N") }, Mes = "登录成功。" };
            }
            if (info.UserName == "xiaoxiao")
            {
                menu.Add(new Menu { Path = "/home", Name = "home", Label = "首页", Icon = "house", Url = "home/Home" });
                menu.Add(new Menu { Path = "/mall", Name = "mall", Label = "商品管理", Icon = "video-play", Url = "mall/Index" });
                return new Res { Code = 200, Data = new Data { Menu = menu, Token = Guid.NewGuid().ToString("N") }, Mes = "登录成功。" };
            }

            return new Res { Code = 300, Data = new Data { }, Mes = "密码错误。" };
        }

        private List<UserInfo> _list = new List<UserInfo>();
    }
}